package com.cangmaomao.m_penManage.presenter

import android.bluetooth.BluetoothGatt
import com.cangmaomao.lib.utils.e
import com.cangmaomao.m_penManage.config.device
import com.cangmaomao.m_penManage.config.state
import com.cangmaomao.m_penManage.contract.PenManageContract
import com.clj.fastble.BleManager
import com.clj.fastble.callback.BleGattCallback
import com.clj.fastble.callback.BleScanCallback
import com.clj.fastble.data.BleDevice
import com.clj.fastble.exception.BleException


class PenMangerPresenter(val view: PenManageContract.ManageView) : PenManageContract.ManagePresenter {


    init {
        view.setPresenter(this)
    }

    override fun start() {
        startScan()
    }


    override fun startScan() {
        BleManager.getInstance().scan(object : BleScanCallback() {

            override fun onScanStarted(success: Boolean) {

            }


            override fun onLeScan(bleDevice: BleDevice?) {
                super.onLeScan(bleDevice)

            }


            override fun onScanning(bleDevice: BleDevice) {
                view.devices(bleDevice)
            }

            override fun onScanFinished(scanResultList: List<BleDevice>) {

            }

        })
    }

    override fun connect(mac: String) {
        BleManager.getInstance().connect(mac, object : BleGattCallback() {
            override fun onStartConnect() {
            }


            override fun onConnectFail(bleDevice: BleDevice, exception: BleException) {
            }


            override fun onConnectSuccess(bleDevice: BleDevice, gatt: BluetoothGatt, status: Int) {
                view.connectState(status == 0, bleDevice)
            }


            override fun onDisConnected(isActiveDisConnected: Boolean, bleDevice: BleDevice, gatt: BluetoothGatt, status: Int) {
            }
        })
    }


    override fun checkBleState(): Boolean {
        return state == 0 && device != null
    }


}